//
//  TDriverViewModel.m
//  LoveShare
//
//  Created by ways on 2017/7/6.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "TDriverViewModel.h"

@implementation TDriverViewModel

- (instancetype)initDriverViewModel {
    self = [super init];
    
    if (self) {
        
        [self initialBind];
    }
    
    return self;
}


#pragma mark - init


- (TFindDriverListModel *)findDriverListModel{
    if (!_findDriverListModel) {
        _findDriverListModel = [[TFindDriverListModel alloc] init];
        _findDriverListModel.page = @"1";//默认首先请求第1页
        _findDriverListModel.line = @"10";//默认每页请求10条
    }
    return _findDriverListModel;
}

- (TReleaseFindDriverModel *)releaseFindDriverModel{
    if (!_releaseFindDriverModel) {
        _releaseFindDriverModel = [[TReleaseFindDriverModel alloc] init];
    }
    return _releaseFindDriverModel;
}

- (void)initialBind{
    
    
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            [params setValue:[TUserManager defaultManager].userInfoModel.result.userId forKey:@"userId"];
            if ([self.requestURL isEqualToString:T_GetFindCarAllInfo]) {
                [params setValue:self.findDriverListModel.page forKey:@"page"];
                [params setValue:self.findDriverListModel.line forKey:@"line"];
            } else if ([self.requestURL isEqualToString:T_PublishFindCar]) {
                /*
                 {"clientInfo":{"appVersion":"1.0","os":"ios"},"authority":{"token":"abcdefg"},
                 "params":{"userId":5,"startPlace":"花园正门","destination":"香雪公园地铁站","goTime":1499087678000,"passenger":2,"message":"我们两个人，有点行旅","contactMobile":13826810198}}
                 */
                [params setValue:self.releaseFindDriverModel.startPlace forKey:@"startPlace"];
                [params setValue:self.releaseFindDriverModel.destination forKey:@"destination"];
                [params setValue:self.releaseFindDriverModel.goTime forKey:@"goTime"];
                [params setValue:self.releaseFindDriverModel.passenger forKey:@"passenger"];
                [params setValue:self.releaseFindDriverModel.message forKey:@"message"];
                [params setValue:self.releaseFindDriverModel.contactMobile forKey:@"contactMobile"];
                
            }
            //
            NSMutableDictionary *authority = [NSMutableDictionary dictionary];
            [authority setValue:@"abcdefg" forKey:@"token"];
            NSMutableDictionary *paramsBig = [NSMutableDictionary dictionary];
            [paramsBig setValue:params forKey:@"params"];
            [paramsBig setValue:authority forKey:@"authority"];
            NSData *body = [NSJSONSerialization dataWithJSONObject:paramsBig options:NSJSONWritingPrettyPrinted error:nil];
            
            [TServiceTool POST:self.requestURL body:body parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary  *_Nullable responseObject) {
                //                NSLog(@"responseObject:%@",responseObject);
                
                if ([responseObject isKindOfClass:[NSDictionary class]]) {
                    NSString *message;
                    if ([self.requestURL isEqualToString:T_GetFindCarAllInfo]) {
                        
                        [self.findDriverListModel mj_setKeyValues:responseObject];
                        message = self.findDriverListModel.message;
                        //
                        [subscriber sendNext:self.findDriverListModel];
                        
                    } else if ([self.requestURL isEqualToString:T_PublishFindCar]) {
                        
                        [self.releaseFindDriverModel mj_setKeyValues:responseObject];
                        message = self.releaseFindDriverModel.message;
                        //
                        [subscriber sendNext:self.releaseFindDriverModel];
                    }
                    
                    if ([responseObject[@"msgCode"] intValue] != 100) {//失败
                        [SVProgressHUD dismissWithDelay:0.5];
                        [SVProgressHUD showErrorWithStatus:message];
                    }
                    
                } else {
                    NSLog(@"返回数据不是JSON");
                    [subscriber sendNext:nil];
                }
                
                // 数据传送完毕，必须调用完成，否则命令永远处于执行状态
                [subscriber sendCompleted];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"error:%@",error.description);
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                [subscriber sendError:error];
                [SVProgressHUD showErrorWithStatus:@"服务器异常"];
            }];
            
            return nil;
            
        }];
        
        
        return signal;
    }];
    
    //监听登录产生的数据
    [_requestCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        
    }];
    
    // 监听登录状态
    @weakify(self)
    [[_requestCommand.executing skip:1] subscribeNext:^(id x) {
        @strongify(self)
        if ([x isEqualToNumber:@(YES)]) {
            
            // 用蒙版提示
            
            
        }else {
            
            //隐藏蒙版

            
            
        }
    }];
}
@end
